package com.ldc.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import org.springframework.boot.json.JacksonJsonParser;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 功能描述
 *
 * @author liudi
 * @since 2023/7/23
 */
public class StringUtils {

    public static boolean isEmpty(String obj) {
        return obj == null || obj.isEmpty();
    }


    public static boolean isNotEmpty(String obj) {
        return !isEmpty(obj);
    }


    public static String toString(Object obj) {
        return String.valueOf(obj);
    }


    public static boolean equals(String oldString, String string) {
        if (isEmpty(oldString) || isEmpty(string)) {
            return false;
        }
        return oldString.equals(string);
    }

    public static boolean equalsIgnoreCase(String oldString, String string) {
        if (isEmpty(oldString) || isEmpty(string)) {
            return false;
        }
        return oldString.equalsIgnoreCase(string);
    }


    public static <T> String toJson(T data) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            return "{}";
        }
    }


    public static <T> T getValueFromJson(Class<T> clazz, String json) {
        try {
            if (isEmpty(json)) {
                return null;
            }
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(json, clazz);
        } catch (JsonProcessingException e) {
            return null;
        }

    }

    /**
     * 获取数据
     *
     * @param inputStream 输入流
     * @return String
     */
    public static String getMessage(InputStream inputStream) {
        try {
            StringBuilder sb = new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            return sb.toString();

        } catch (Exception error) {
            return "";
        }
    }
}
